package com.c4p.service;

import com.c4p.entity.CustomUser;
import com.c4p.repository.CustomUserRepository;
import lombok.AllArgsConstructor;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class CustomUserDetailService implements UserDetailsService {

    private CustomUserRepository customUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        CustomUser user = this.customUserRepository.findByUsername(username);
        if (user == null){
            throw new UsernameNotFoundException(username + " not found ");
        }

        return new User(username, user.getPasswd(), AuthorityUtils.createAuthorityList(username));
    }
}
